// pages/consumeRecord/consumeRecord.js
var app = getApp();
var page_s = 1;
var page2_s = 1;
var pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conditions1: {
      start_time: '',
      end_time: '',
    },

    conditions2: {
      start_time: '',
      end_time: '',
    },

    drawer: false,
    isFilter: false,

    rechargeList: [],
    costList: [],
    row: 10,
    id: '',
    currentTab: 0, //0是充值记录  1消费记录
    has_more: false, //数据是否加载完成
    load_ing: false, //加载动画
    is_load: false, //是否加载
    //2是消费  的控制
    has2_more: false, //数据是否加载完成
    load2_ing: false, //加载动画
    is2_load: false, //是否加载
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //console.log(options.id)
    that.setData({
      id: options.id
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    page_s = 1;
    that.setData({
      rechargeList: []
    })
    page_s = 1;
    that.getRechargeRecord(page_s);
    that.setData({
      costList: []
    })
    page2_s = 1;
    that.getcostRecord(page2_s);
  },

  // 获取充值记录
  getRechargeRecord(page_s) {
    var that = this;

    let salesman_id = wx.getStorageSync('salesman_id');
    app.ajax({
      url: 'Partner/WaterCard/card_recharge_record',
      data: {
        water_id: that.data.id,
        page: page_s,
        row: pageSize,
        ...that.data.conditions1
      },
      success: function (res) {
        //console.log(res); 
        var getData = res.data.data.data;
        console.log('数据', getData);
        var h_more = true;
        if (getData.length < pageSize) {
          h_more = false;
        }
        that.setData({
          has_more: h_more,
          load_ing: false,
          drawer:false
        })
        if (res.data.code == 1000) {
          var changeList = [];
          if (that.data.rechargeList == "") {
            changeList = res.data.data.data;
          } else {
            var addList = that.data.rechargeList;
            changeList = addList.concat(res.data.data.data);
          }
          that.setData({
            rechargeList: changeList,
          })
        }
        wx.hideLoading();
        that.setData({
          is_load: true
        })
      },
    })
  },

  // 获取消费记录
  getcostRecord(page2_s) {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/waterPurchaseHistory',
      data: {
        id: that.data.id,
        page: page2_s,
        row: pageSize,
        ...that.data.conditions2
      },
      success: function (res) {
        var getData = res.data.data.data;
        console.log('数据', getData);
        var h2_more = true;
        if (getData.length < pageSize) {
          h2_more = false;
        }
        that.setData({
          has2_more: h2_more,
          load2_ing: false,
          drawer:false
        })
        if (res.data.code == 1000) {
          var change_List = [];
          if (that.data.costList == "") {
            change_List = res.data.data.data;
          } else {
            var addList = that.data.costList;
            change_List = addList.concat(res.data.data.data);
          }
          that.setData({
            costList: change_List,
          })
        }
        wx.hideLoading();
        that.setData({
          is2_load: true
        })
      },
    })
  },

  // 上拉加载
  reachBottom: function () {
    var that = this;
    if (that.data.currentTab == 0) {
      // 当没有数据时,不再请求 --------- 充值记录
      var h_more = that.data.has_more;
      if (!h_more) {
        return false;
      }
      that.setData({
        load_ing: true
      })
      page_s += 1;
      that.getRechargeRecord(page_s);
    } else if (that.data.currentTab == 1) {
      // 当没有数据时,不再请求 --------- 消费记录
      var h2_more = that.data.has2_more;
      if (!h2_more) {
        return false;
      }
      that.setData({
        load2_ing: true
      })
      page2_s += 1;
      that.getcostRecord(page2_s);
    }
  },

  // 切换选项卡
  switchtab: function (e) {
    const index = e.target.dataset.current;
    if (this.data.currentTab === index) {
      return false;
    } else {
      this.setData({
        currentTab: index,
      })
    }
  },




  showDrawer() {
    this.condition = {
      ...this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      [`${this.data.currentTab ==0?'conditions1':'conditions2'}`]: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      [`${this.data.currentTab ==0 ?'conditions1':'conditions2'}`]: conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);

    if(this.data.currentTab==0){
      this.setData({
        rechargeList: []
      })
      page_s = 1;
      this.getRechargeRecord(page_s);
    }else{
      this.setData({
        costList: []
      })
      page2_s = 1;
      this.getcostRecord(page2_s);
    }
  },



  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    let pname = this.data.currentTab == 0 ? 'conditions1' : 'conditions2'
    this.setData({
      [`${pname}.${name}`]: e.detail.value
    })
  },


})